import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from supervised_lenet import inference
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
mnist=read_data_sets("MNIST_data/",one_hot=True)

#加载方法1：
image = tf.placeholder(tf.float32,[None,784],name='input_x')
y_ = tf.placeholder(tf.float32,[None,10],name='input_y')
keep_prob = tf.placeholder(tf.float32,name='prob')
loss,logits = inference(image,y_,keep_prob)
# logits = vgg16(image,keep_prob,train_flag)
saver = tf.train.Saver()

pred_val = tf.nn.softmax(logits)

with tf.Session() as sess:
    saver.restore(sess,tf.train.latest_checkpoint('./supervised_model'))
    print("finish loading model!")

    per_dataset = np.load("./supervised_test_picture.npy")
    test_image = per_dataset[3].reshape(1,784)
    test_label = mnist.test.labels[3]
    pred_label = np.argmax(sess.run(logits, feed_dict={image: test_image, keep_prob: 1.0}))
    print("预测标签为：", pred_label)
    print("真实标签为：", np.argmax(test_label))

    pred = sess.run(pred_val,feed_dict={image:test_image,keep_prob:1.0}).squeeze()
    pred_l = np.argsort(pred)
    acc = pred[pred_l[9]]*100
    print("预测置信度为：",str(acc))

    plt.figure()
    plt.imshow(test_image.reshape(28,28),cmap='gray')
    plt.show()